package software

import (
	"fmt"
	"github.com/beherochuling/beesoft_api/app/service/util"
	"io"
	"io/ioutil"
	"net/http"

	model "github.com/beherochuling/beesoft_api/app/model/software"
)

type serviceKong struct {
	uri string
}

func (r *serviceKong) Uri(uri string) *serviceKong {
	r.uri = uri

	return r
}
func (r *serviceKong) Request(method, uri string, requestBody io.Reader) []byte {
	uri = fmt.Sprintf("%s%s", r.uri, uri)
	req, _ := http.NewRequest(method, uri, requestBody)

	client := &http.Client{}
	res, err := client.Do(req)
	if err != nil {
		panic(err)
	}
	// fmt.Println(res)

	defer func(Body io.ReadCloser) {
		err := Body.Close()
		if err != nil {
			panic(err)
		}
	}(res.Body)

	body, err := ioutil.ReadAll(res.Body)
	if err != nil {
		panic(err)
	}
	// fmt.Println(string(body))

	return body
}

func (r *serviceKong) Root() []byte {
	return r.Request("GET", "/", nil)
}

// kong config init
func (r *serviceKong) Config() []byte {
	return r.Request("POST", "/config", nil)
	// config
	// check_hash
}
func (r *serviceKong) Endpoints() []byte {
	return r.Request("GET", "/endpoints", nil)
}
func (r *serviceKong) Head()    {}
func (r *serviceKong) Options() {}
func (r *serviceKong) SchemasEntityValidate() []byte {
	return r.Request("POST", "/schemas/{entity}/validate", nil)
}

func (r *serviceKong) SchemasPluginsValidate() []byte {
	return r.Request("POST", "/schemas/plugins/validate", nil)
}

func (r *serviceKong) SchemasValidate() []byte {
	return r.Request("POST", "/schemas/{entity}/schemas/{实体名称}", nil)
}
func (r *serviceKong) SchemasValidate1() []byte {
	return r.Request("POST", "/schemas/plugins/{plugin}", nil)
}
func (r *serviceKong) Timers() []byte {
	return r.Request("POST", "/timers", nil)
}

// ## Health 健康

func (r *serviceKong) Status() *model.KongStatus {
	response := r.Request("GET", "/status", nil)

	var result model.KongStatus
	err := util.JsonToStruct(response, &result)
	if err != nil {
		panic(err)
	}

	return &result
}

// ## Tags 标签

func (r *serviceKong) Tags() []byte {
	return r.Request("GET", "/tags", nil)
}
func (r *serviceKong) TagsShow() []byte {
	return r.Request("GET", "/tags/{tags}", nil)
}

// ## Debug 调试

func (r *serviceKong) ClusterControlPlanesNodesLogLevelSet() []byte {
	return r.Request("PUT", "/debug/cluster/control-planes-nodes/log-level/{log_level}", nil)
}
func (r *serviceKong) ClusterLogLevelSet() []byte {
	return r.Request("PUT", "/debug/cluster/log-level/{log_level}", nil)
}
func (r *serviceKong) NodeLogLevelGet() []byte {
	return r.Request("GET", "/debug/node/log-level", nil)
}
func (r *serviceKong) DebugNodeLogLevelSet() []byte {
	return r.Request("PUT", "/debug/node/log-level/{log_level}", nil)
}

// ## Services 服务

func (r *serviceKong) Services() []byte {
	return r.Request("GET", "/services", nil)
	// certificates
}
func (r *serviceKong) ServiceCreate() []byte {
	return r.Request("POST", "/services", nil)
	// certificates
}
func (r *serviceKong) ServiceDelete(serviceName string) []byte {
	uri := fmt.Sprintf("/services/%s", serviceName)
	return r.Request("DELETE", uri, nil)
	// certificates
}
func (r *serviceKong) ServiceUpdate() []byte {
	return r.Request("PATCH", "/services", nil)
	// certificates
	// routes
	// plugins
}
func (r *serviceKong) ServiceStore() []byte {
	return r.Request("PUT", "/services", nil)
	// certificates
	// routes
	// plugins
}
func (r *serviceKong) ServiceShow(serviceName string) []byte {
	uri := fmt.Sprintf("/services/%s", serviceName)
	return r.Request("GET", uri, nil)
	// certificates
	// routes
	// plugins
}

// ## Routes 路由

func (r *serviceKong) Routes() []byte {
	return r.Request("GET", "/routes", nil)
	// /services
	// /plugins/{plugin id}/route
}
func (r *serviceKong) RouteCreate(serviceName string) []byte {
	// /services
	uri := fmt.Sprintf("/services/%s/routes", serviceName)
	return r.Request("POST", uri, nil)
}
func (r *serviceKong) RouteDelete(routeName string) []byte {
	uri := fmt.Sprintf("/routes/%s", routeName)
	return r.Request("DELETE", uri, nil)
}
func (r *serviceKong) RouteUpdate() []byte {
	return r.Request("PATCH", "/services", nil)
	// plugins
}
func (r *serviceKong) RouteStore() []byte {
	return r.Request("PUT", "/services", nil)
	// plugins
}
func (r *serviceKong) RouteShow(serviceName string) []byte {
	uri := fmt.Sprintf("/services/%s", serviceName)
	return r.Request("GET", uri, nil)
	// plugins
}

func (r *serviceKong) Consumers() []byte {
	return r.Request("GET", "/consumers", nil)
	// /services
	// /plugins/{plugin id}/route
}
func (r *serviceKong) ConsumerCreate(serviceName string) []byte {
	// /services
	uri := fmt.Sprintf("/consumers/%s/routes", serviceName)
	return r.Request("POST", uri, nil)
}
func (r *serviceKong) ConsumerDelete(routeName string) []byte {
	uri := fmt.Sprintf("/consumers/%s", routeName)
	return r.Request("DELETE", uri, nil)
}
func (r *serviceKong) ConsumerUpdate() []byte {
	return r.Request("PATCH", "/consumers", nil)
	// plugins
}
func (r *serviceKong) ConsumerStore() []byte {
	return r.Request("PUT", "/consumers", nil)
	// plugins
}
func (r *serviceKong) ConsumerShow(serviceName string) []byte {
	uri := fmt.Sprintf("/consumers/%s", serviceName)
	return r.Request("GET", uri, nil)
	// plugins
}

// ## Plugins 插件

func (r *serviceKong) Plugins() []byte {
	return r.Request("GET", "/plugins", nil)
	// /services
	// /plugins/{plugin id}/route
}
func (r *serviceKong) PluginCreate(scope string) []byte {
	uri := fmt.Sprintf("/plugins")
	return r.Request("POST", uri, nil)
}
func (r *serviceKong) PluginDelete(routeName string) []byte {
	uri := fmt.Sprintf("/plugins/%s", routeName)
	return r.Request("DELETE", uri, nil)
}
func (r *serviceKong) PluginUpdate() []byte {
	return r.Request("PATCH", "/plugins", nil)
	// plugins
}
func (r *serviceKong) PluginStore() []byte {
	return r.Request("PUT", "/plugins", nil)
	// plugins
}
func (r *serviceKong) PluginShow(serviceName string) []byte {
	uri := fmt.Sprintf("/plugins/%s", serviceName)
	return r.Request("GET", uri, nil)
	// plugins
}

// ## Certifications 证书

func (r *serviceKong) CertificationCreate() {

}
